package com.fjh.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 检查用户是否已登录
        if (isUserLoggedIn(request)) {
            return true; // 继续请求处理
        } else {
            // 用户未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/users/login");
            return false; // 中断请求处理
        }
    }



    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在整个请求完成后执行，通常用于资源清理工作
    }

    private boolean isUserLoggedIn(HttpServletRequest request) {
        // 实现检查用户是否已登录的逻辑
        return request.getSession().getAttribute("loggedInUser") != null;
    }

}
